<?php
namespace app\mobile\controller;

use app\model\Theme; 
use app\model\Worker; 

class WFansrank extends Base
{
    public function W_fansrank()
    {
global $_G, $_P;
$fans = $this->checkuser();
$themeid = intval($_P['themeid']);
if($themeid < 0){
	return_msg('管理后台地址不正确');
}
$system=$_G['system'];
$theme = Theme::getThemeById($themeid);
$config = 	Theme::getConfig($themeid);
if(empty($theme['id'])){
	return_msg('管理后台地址不正确');
}
$myworker = Worker::getThemeWorkerByOpenidNew($_G['openid'],$themeid);
if(!($system['ruanjian']!=3 || $config['showshop']==0 || ($system['ruanjian']==3 && $config['showshop']==$myworker['company']))){
	$myworker = "";
}
if (empty($myworker)) {
	return_msg('您不是员工,无权访问');
}
$msg = Worker::gethoutaistats();
if (!empty($msg)) {
	return_msg($msg);
}

$do='w_fansrank';


	$paixu = $_P['paixu'];
	// }
	if ($paixu=='ll') {
		$sortinfo = '粉丝浏览量排名';
		$sorttxt = ' order by scansum desc,id desc';
	}elseif ($paixu=='zf') {
		$sortinfo = '粉丝转发量排名';
		$sorttxt = ' order by forward desc,id desc';
	}elseif ($paixu=='tj') {
		$sortinfo = '粉丝推荐排名';
		$sorttxt = ' order by responsum desc,id desc';
	}elseif ($paixu=='cj') {
		$sortinfo = '粉丝成交排名';
		$sorttxt = ' order by tuijian desc,id desc';
		db_update(t_fans, array('tuijian' => 0 ));
		$record = db_select("SELECT fromuid FROM ".tablename(t_record)."
                            WHERE themeid =" . $themeid ." and status > 0 and fromuid > 0 order by id desc");
		foreach ($record as $index => $item) {
        	db_query("UPDATE ".tablename(t_fans)." SET tuijian = tuijian + 1 WHERE themeid = '{$themeid}' and uid ={$item['fromuid']}");
		}
	}else{
		$sortinfo = '最新粉丝';
		$sorttxt = ' order by id desc';
	}
	$fansranklist = db_select("SELECT * FROM ".tablename(t_fans)."
        WHERE themeid =" . $themeid ." ". $sorttxt ." limit 100");

if($_G['companyname'] =='新飞驰软件'){
	$return_clicent = get_defined_vars();
return view('/fcworker/fc_fansrank' , $return_clicent);
}else{
	$return_clicent = get_defined_vars();
return view('/yxworker/w_fansrank' , $return_clicent);
}

}
}